package com.jbm.jbmsupplier.frement;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.DispushActivity;
import com.jbm.jbmsupplier.activity.NewHomeActivity;
import com.jbm.jbmsupplier.activity.activitymanag.Activity_Manage;
import com.jbm.jbmsupplier.adapter.OrderAdapter;
import com.jbm.jbmsupplier.presenters.ListFrements;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;
import com.jbm.jbmsupplier.views.ListFreViews;
import com.jbm.onehome.iface.JobOrderEntity;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 家帮忙 on 2017/4/10.
 */

public class ListFrement extends Fragment implements NewHomeActivity.getDateType,ListFreViews,Activity_Manage.getDateTypes
{
    @InjectView(R.id.pull_refresh_list)
    PullToRefreshListView pullRefreshList;
    @InjectView(R.id.lv_defaultorder)
    ImageView lvDefaultorder;
    private boolean isVisibleToUser1;
  private ListFrements listFrements;
    private OrderAdapter orderAdapter;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.lz_frement, null);
        ExitActivity.getInstance().addActivity(getActivity());
         ButterKnife.inject(this, view);
          listFrements=new ListFrements(getActivity(),this);
           listFrements.setInitData(isVisibleToUser1);
          pullRefreshList.setEmptyView(lvDefaultorder);
          pullUpRefresh();
        return view;
    }
    private void pullUpRefresh() {
        pullRefreshList.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {//预加载
                listFrements.setLoadLaunch(1);
            }
        });
        pullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉
                listFrements.setLoadLaunch(2);
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉
               listFrements.setLoadLaunch(3);
            }

        });
        pullRefreshList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JobOrderEntity or = (JobOrderEntity) parent.getAdapter().getItem(position);
                if (!or.getStatus().equals("new")) {
                    Intent intent = new Intent(getActivity(), DispushActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("order", or);
                    bundle.putString("tag", "2");
                    intent.putExtras(bundle);
                    //标志当前是哪个页面
                    startActivity(intent);
                }


            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisibleToUser1 = isVisibleToUser;

    }

    @Override
    public void setType(int i, String statiu) {
        listFrements.getOrderType(i,statiu);
    }



    @Override
    public void setSucced(DialogLoading dialogloading,ArrayList<JobOrderEntity> listJorider) {//成功
        if (dialogloading!=null){
            dialogloading.dismiss();
        }
        if (orderAdapter==null){
            orderAdapter = new OrderAdapter(getActivity(), listJorider);
            if (pullRefreshList != null) {
                pullRefreshList.setAdapter(orderAdapter);

            }
        }else
        if (pullRefreshList != null) {
            pullRefreshList.onRefreshComplete();
        }

    }

    @Override
    public void setError(String err, DialogLoading dialogloading) {//失败
        if (dialogloading!=null){
            dialogloading.dismiss();
        }
        if (pullRefreshList != null) {
            pullRefreshList.onRefreshComplete();
        }
        erreor(err);
    }

    @Override
    public void setOrderData(ArrayList<JobOrderEntity> listJorider,int tag) {//获取数据
        if (tag==1){//代表第一次加载数据
        orderAdapter = new OrderAdapter(getActivity(), listJorider);
        if (pullRefreshList != null) {
            pullRefreshList.setAdapter(orderAdapter);
        }
        }else if (tag==2){//更新数据
            if(orderAdapter!=null){
            orderAdapter.setNotifin(listJorider);
            }else{
                orderAdapter = new OrderAdapter(getActivity(), listJorider);
                if (pullRefreshList != null) {
                    pullRefreshList.setAdapter(orderAdapter);
                }
            }
        }
    }

    @Override
    public void setLoadLayout() {//刷新
        if (pullRefreshList != null) {
            ILoadingLayout startLabels = pullRefreshList.getLoadingLayoutProxy();
            startLabels.setRefreshingLabel("您已到底");// 刷新时
        }
    }

    @Override
    public void setUpdata(ArrayList<JobOrderEntity> listJorider) {//0数据更新
        if (orderAdapter != null) {
            orderAdapter.notifyDataSetChanged();
        }else{
            orderAdapter = new OrderAdapter(getActivity(), listJorider);
            orderAdapter.notifyDataSetChanged();
        }
    }

    public void erreor(String str) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(getActivity(), str);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();

            }
        });
        dialog.setisVisibility();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        listFrements.onDestroy();
    }






}
